<?php
// +----------------------------------------------------------------------
// | Author: huangzhen_20210406
// +----------------------------------------------------------------------

namespace app\common\model;

use think\facade\Db;

class UserSet extends Common
{
    /**
     * 用过user访问user child
     * @param $post
     * @param $user_id
     * @return mixed
     * @throws \think\db\exception\DbException
     */
    public function tableDataAdmin($post)
    {
        $list_rows  = $post['limit'] ?? config('paginate.list_rows');
        $page       = $post['page'] ?? config('paginate.page');

        $where =[];
        if(isset($post['username']) && !empty($post['username'])){
            $where[] = ['u.username', 'like', '%' . trim($post['username']) . '%'];
        }
        if(isset($post['datetime']) && !empty($post['datetime'])){
            $time_list = explode('_' , $post['datetime']);
            $start_time = strtotime($time_list[0] . " 00:00:00");
            $end_time   = strtotime($time_list[1] . " 23:59:59");
            $where[] = ['uf.ctime', 'between', "{$start_time},{$end_time}"];
        }
        $list = Db::name('user_set')->alias('uf')
                ->leftJoin("user u", "u.id=uf.user_id")
                ->field('uf.* , u.username')
                ->where($where)
                ->order('uf.id desc')
                ->paginate(['list_rows' => $list_rows, 'page' => $page])
                ->toArray();
        if($list){
            foreach ($list['data'] as $k=>&$v){
                $card_set = json_decode($v['card_set'] , true);
                foreach ($card_set as $kk=>$vv){
                    $v['fund_' . $vv['bank_type'] . '_' . $vv['card_type']] = $vv['rate_fund'];
                    $v['open_' . $vv['bank_type'] . '_' . $vv['card_type']] = $vv['open_amount'];
                    $v['refund_' . $vv['bank_type'] . '_' . $vv['card_type']] = $vv['rate_refund'];
                    $v['use_' . $vv['bank_type'] . '_' . $vv['card_type']] = $vv['is_used'];
                }
            }
        }
        return $result = [
            'code'  => 0,
            'msg'   => lang('get info success'),
            'data'  => $list['data'],
            'count' => $list['total'],
        ];
    }

    public static function changeCardSet($data)
    {
        $set_id       = $data['set_id'];
        if($set_id == 0){
            $list_default = Db::name('user_set')->where(['id' => 1])->find();
        }else{
            $list_default = Db::name('user_set')->where(['id' => $set_id])->find();
        }
        $old_max_card       = $list_default['max_card'];
        $old_zero_auth      = $list_default['zero_auth'];
        $old_facebook       = $list_default['facebook'];
        $old_declined       = $list_default['declined'];
        $cardSetList        = json_decode($list_default['card_set'] , true);
        $list_new           = [];
        $list_change        = [];

        foreach ($cardSetList as $k=>$v){
            $bank_type  = $v['bank_type'];
            $card_type  = $v['card_type'];
            $open_amount= $v['open_amount'];
            $rate_fund  = $v['rate_fund'];
            $rate_refund= $v['rate_refund'];
            $rate_tran  = $v['rate_tran'];
            $is_used    = $v['is_used'];
            $is_refe    = $v['is_refe'];

            $open_amount_new = trim($data[$bank_type . '_' . $card_type . '_open_amount']);
            $rate_fund_new   = trim($data[$bank_type . '_' . $card_type . '_rate_fund']);
            $rate_refund_new = trim($data[$bank_type . '_' . $card_type . '_rate_refund']);
            $rate_tran_new   = trim($data[$bank_type . '_' . $card_type . '_rate_tran']);
            $is_used_new     = $data[$bank_type . '_' . $card_type . '_is_used'];
            $is_refe_new     = $data[$bank_type . '_' . $card_type . '_is_refe'];

            if($open_amount - $open_amount_new != 0){
                $list_change[$bank_type][$card_type]['open_amount'] = "{$open_amount} => {$open_amount_new}";
            }
            if($rate_fund - $rate_fund_new != 0){
                $list_change[$bank_type][$card_type]['rate_fund'] = "{$rate_fund} => {$rate_fund_new}";
            }
            if($rate_refund - $rate_refund_new != 0){
                $list_change[$bank_type][$card_type]['rate_refund'] = "{$rate_refund} => {$rate_refund_new}";
            }
            if($rate_tran - $rate_tran_new != 0){
                $list_change[$bank_type][$card_type]['rate_tran'] = "{$rate_tran} => {$rate_tran_new}";
            }
            if($is_used != $is_used_new){
                $start = ($is_used ==1)?'停用':'不停用';
                $end   = ($is_used_new ==1)?'停用':'不停用';
                $list_change[$bank_type][$card_type]['is_used'] = "{$start} => {$end}";
            }
            if($is_refe != $is_refe_new){
                $start_refe = ($is_refe ==1)?'推荐':'不推荐';
                $end_refe   = ($is_refe_new ==1)?'推荐':'不推荐';
                $list_change[$bank_type][$card_type]['is_refe'] = "{$start_refe} => {$end_refe}";
            }
            $list_new[] = [
                'bank_type'     => $v['bank_type'],
                'card_type'     => $v['card_type'],
                'open_amount'   => $open_amount_new,
                'rate_fund'     => $rate_fund_new,
                'rate_refund'   => $rate_refund_new,
                'rate_tran'     => $rate_tran_new,
                'is_used'       => $is_used_new,
                'is_refe'       => $is_refe_new,
            ];
        }
        $ctime  = getUtcTime();
        if($set_id == 0){
            $re = Db::name('user_set')
                ->save([
                    'user_id'   => $data['user_id'],
                    'max_card'  => $data['max_card'],
                    'zero_auth' => $data['zero_auth'],
                    'facebook' => $data['facebook'],
                    'declined' => $data['declined'],
                    'type'      => 2,
                    'card_set'  => json_encode($list_new),
                    'ctime'     => $ctime
                ]);
            Db::name('user_set_log')
                ->save([
                    'manage_id' => $data['manage_id'],
                    'user_id'   => $data['user_id'],
                    'type'      => 0,
                    'data'      => json_encode($list_new + ['max_card' => "{$old_max_card} => " . $data['max_card']]),
                    'ctime'     => $ctime
                ]);
        }else{
            $re = Db::name('user_set')->where(['id' =>$set_id])
                ->update([
                    'max_card' => $data['max_card'],
                    'zero_auth' => $data['zero_auth'],
                    'facebook' => $data['facebook'],
                    'declined' => $data['declined'],
                    'card_set' => json_encode($list_new)
                ]);
            if($old_max_card - $data['max_card'] != 0){
                $list_change += ['max_card' => "{$old_max_card} => " . $data['max_card']];
            }
            if($old_zero_auth - $data['zero_auth'] != 0){
                $list_change += ['zero_auth' => "{$old_zero_auth} => " . $data['zero_auth']];
            }
            if($old_facebook - $data['facebook'] != 0){
                $list_change += ['facebook' => "{$old_facebook} => " . $data['facebook']];
            }
            if($old_declined - $data['declined'] != 0){
                $list_change += ['declined' => "{$old_declined} => " . $data['declined']];
            }
            if(!empty($list_change)){
                Db::name('user_set_log')
                    ->save([
                        'manage_id' => $data['manage_id'],
                        'user_id'   => $list_default['user_id'],
                        'type'      => 1,
                        'data'      => json_encode($list_change),
                        'ctime'     => $ctime
                    ]);
            }

        }
        return $re;
    }

    public static function getCardSet($user_id)
    {
        $list = Db::name('user_set')->where(['user_id' => $user_id])
            ->order('type')->find();
        if(empty($list)){
            $type  = Db::name('user')->where(['id' => $user_id])->value('grade');
            if($type == 2){
                $type =1;
            }
            $list = Db::name('user_set')->where(['type' => $type , 'user_id'=>0])->find();
        }
        $list_new =[];
        $list_new['max_card'] = $list['max_card'];
        $card_set = json_decode($list['card_set'] , true);
        foreach ($card_set as $k=>$v){
            $list_new[$v['bank_type']][$v['card_type']]['is_used'] = $v['is_used'];
            $list_new[$v['bank_type']][$v['card_type']]['rate_fund'] = $v['rate_fund'];
            $list_new[$v['bank_type']][$v['card_type']]['rate_refund'] = $v['rate_refund'];
            $list_new[$v['bank_type']][$v['card_type']]['rate_tran'] = $v['rate_tran'];
            $list_new[$v['bank_type']][$v['card_type']]['open_amount'] = $v['open_amount'];
        }
        return $list_new;
    }

}